//登录的路由处理函数
//导入数据库连接
const db = require('../db/index')
//导入生成token的包
const jwt = require('jsonwebtoken')
//导入生成token的密钥
const secret = require('../utlis/secret')


module.exports.login = (req, res) => {
  const userInfo = req.query
  console.log(userInfo);
  const sqlStr = 'SELECT * FROM Administer WHERE username=?'
  db.query(sqlStr, userInfo.username, (err, result) => {
    if (err) {
      return res.send({
        status: 1,
        message: '登录失败'
      })
    }
    if (result.length === 0) {
      return res.send({
        status: 1,
        message: '登录失败'
      })
    }
    if (result.length === 1) {
      return res.send({
        status: 200,
        message: '登录成功',
        token: 'Bearer ' + jwt.sign({ username: userInfo.userInfo }, secret, { expiresIn: '24h' })
      })
    }
  })
}